package org.softmed.ATComm.swingClient;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SerialPortPanel extends JPanel implements ActionListener {

	JComboBox ports = new JComboBox();
	JButton readPorts = new JButton("Read port data");

	int serialsWidth = 250;
	int serialsHeight = 25;

	int buttonWidth = 160;
	int buttonsHeight = 25;
	private ClientManager clientManager;

	public SerialPortPanel() {
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		add(new JLabel("Serial Ports   :    "));

		ports.setMaximumSize(new Dimension(serialsWidth, serialsHeight));
		ports.setMinimumSize(new Dimension(serialsWidth, serialsHeight));
		ports.setPreferredSize(new Dimension(serialsWidth, serialsHeight));

		add(ports);

		readPorts.setMaximumSize(new Dimension(buttonWidth, buttonsHeight));
		readPorts.setMinimumSize(new Dimension(buttonWidth, buttonsHeight));
		readPorts.setPreferredSize(new Dimension(buttonWidth, buttonsHeight));

		add(readPorts);
		add(Box.createHorizontalGlue());

		readPorts.addActionListener(this);
	}

	public String getPort() {
		String port = (String) ports.getSelectedItem();
		return port;
	}

	public void setPort(String port) {
		ports.setSelectedItem(port);
	}

	public void setPorts(List<String> portList) {
		ports.removeAllItems();
		for (String port : portList) {
			ports.addItem(port);
		}

	}

	public void setClientManager(ClientManager clientManager) {
		this.clientManager = clientManager;

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		try {
			clientManager.readServerPortConfig();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
